package com.gemframework.modules.extent;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gemframework.common.utils.GemBeanUtils;
import com.gemframework.model.common.BaseResultData;
import com.gemframework.model.common.PageInfo;
import com.gemframework.model.entity.po.Count;
import com.gemframework.model.entity.po.Member;
import com.gemframework.model.entity.po.Order;
import com.gemframework.model.entity.po.Users;
import com.gemframework.model.entity.vo.OrderVo;
import com.gemframework.model.entity.vo.UsersVo;
import com.gemframework.model.enums.ExceptionCode;
import com.gemframework.service.MemberService;
import com.gemframework.service.OrderService;
import com.gemframework.service.UsersService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/Order")
public class OrderAPI {
    @Resource
    private OrderService orderService;
    @Resource
    private UsersService usersService;

    //订单表id查
    @PostMapping("/selOrderById")
    public OrderVo selOrderById(Integer id){
        OrderVo order=new OrderVo();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        Order byId = orderService.getById(id);
        order = GemBeanUtils.copyProperties(byId, OrderVo.class);
        UsersVo usersVo = usersService.selUsers(order.getUsersId());
        UsersVo usersVo2 = usersService.selUsers(order.getUsersIds());
        order.setUsers(usersVo);
        order.setUseres(usersVo2);
        return order;
    }

    //订单表集合查询家长
    @PostMapping("/selOrderByStudentId")
    public List<OrderVo> selOrderListById(Integer id){
        List<OrderVo> olist=new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("student_id", id);
        List<Order> list = orderService.list(queryWrapper);
        for (Order order : list) {
            OrderVo orderVo = GemBeanUtils.copyProperties(order, OrderVo.class);
            UsersVo usersVo = usersService.selUsers(order.getUsersId());
            UsersVo usersVo2 = usersService.selUsers(order.getUsersIds());
            orderVo.setUsers(usersVo);
            orderVo.setUseres(usersVo2);
            olist.add(orderVo);
        }
        return olist;
    }

    //订单表集合查询老师
    @PostMapping("/selOrderByTeacherId")
    public List<OrderVo> selOrderByTeacherId(Integer id){
        List<OrderVo> olist=new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("teacher_id", id);
        List<Order> list = orderService.list(queryWrapper);
        for (Order order : list) {
            OrderVo orderVo = GemBeanUtils.copyProperties(order, OrderVo.class);
            UsersVo usersVo = usersService.selUsers(order.getUsersId());
            UsersVo usersVo2 = usersService.selUsers(order.getUsersIds());
            orderVo.setUsers(usersVo);
            orderVo.setUseres(usersVo2);
            olist.add(orderVo);
        }
        return olist;
    }

    //查询订单表各种执行状态的数量
    @PostMapping("/select")
    public Map<String,Object> select(@RequestBody Count count){
        Map<String,Object> map=new HashMap<>();
        int select0 = usersService.select(count.getId(),0, count.getCreDate());
        int select1 = usersService.select(count.getId(),1,count.getCreDate());
        int select2 = usersService.select(count.getId(),2,count.getCreDate());
        int select3 = usersService.select(count.getId(),3,count.getCreDate());
        map.put("select0",select0);
        map.put("select1",select1);
        map.put("select2",select2);
        map.put("select3",select3);
        return map;
    }

    //订单添加
    @PostMapping("/insert")
    public BaseResultData insert(@RequestBody Order order){
        return  BaseResultData.SUCCESS(orderService.save(order));
    }

    //订单表查询LoginId
    @PostMapping("/listByTeacherId")
    public int listByTeacherId(Integer id){
        Users u=usersService.getById(id);
        int i = u.getLoginId();
        if(i==0){
            BaseResultData.ERROR(ExceptionCode.DATA_NOT_EXIST);
        }
        return i;
    }

    //修改添加
    @PostMapping("/update")
    public BaseResultData update(@RequestBody Order order){
        return  BaseResultData.SUCCESS(orderService.updateById(order));
    }
}
